package me.zhengjie.modules.quartz.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import me.zhengjie.common.utils.JacksonUtil
import org.hibernate.annotations.CreationTimestamp

import jakarta.persistence.*
import me.zhengjie.modules.system.domain.Area
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.data.redis.core.RedisHash
import java.sql.Timestamp
/**
 * @author Kuki Wu
 * @date 2019-01-07
 */
@Entity
@Table(name = "sys_quartz_log")
@JsonTypeName("QuartzLog")
@RedisHash("quartzlog")
data class QuartzLog(
    @Id
    @Column(name = "log_id")
    @Schema(description = "id", hidden = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,

    // 任务名称
    @Schema(description = "任务名称")
    @Column(name = "job_name")
    var jobName: String? = null,

    // Bean名称
    @Schema(description = "Bean名称")
    @Column(name = "baen_name")
    var beanName: String? = null,

    // 方法名称
    @Schema(description = "方法名称")
    @Column(name = "method_name")
    var methodName: String? = null,

    // 参数
    @Schema(description = "参数")
    @Column(name = "params")
    var params: String? = null,

    // cron表达式
    @Schema(description = "cron表达式")
    @Column(name = "cron_expression")
    var cronExpression: String? = null,

    // 状态
    @Schema(description = "状态")
    @Column(name = "is_success")
    var isSuccess: Boolean? = null,

    // 异常详细
    @Schema(description = "异常详细")
    @Column(name = "exception_detail", columnDefinition = "text")
    var exceptionDetail: String? = null,

    // 耗时（毫秒）
    @Schema(description = "耗时")
    var time: Long? = null,

    // 创建日期
    @Schema(description = "创建日期")
    @CreationTimestamp
    @Column(name = "create_time")
    var createTime: Timestamp? = null
){
    annotation class Update

    companion object {
        fun fromJson(json: String): Area? {
            val obj=JacksonUtil.parseObject(json, Area::class.java)!!
            return when(obj){
                is Area -> obj
                else -> null
            }
        }
    }

    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }

    fun copy(source: QuartzLog?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}